package com.um.jdy.app.activity.manager;


import com.um.jdy.app.activity.entity.dto.MyCouponsPageDTO;
import com.um.jdy.app.activity.entity.vo.CouponsUserItemVO;
import com.um.jdy.app.activity.entity.vo.MyCouponsVO;
import com.um.jdy.common.activity.enums.ActivityEnum;
import com.um.jdy.common.activity.service.CouponsService;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.member.service.MemberUserService;
import com.um.springboot.starter.config.SystemSQL;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.utils.condition.ConditionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 管理类：优惠券用户关联表
 */
@Component
public class CouponsUserManager {


    @Autowired
    protected MemberUserService userService;

    @Autowired
    protected CouponsService couponsService;

    public Page<CouponsUserItemVO> getChargePage(int pageNo, int pageSize, ActivityEnum.CouponsStatus status){
        ConditionBuilder condition = ConditionBuilder.create();
        condition.put("member_user_id", userService.get().getMemberUserId());
        if(null != status){
            switch (status){
                case Unuse:
                    condition.put("is_use", YesOrNo.No.name());
                    condition.put("is_overdue", YesOrNo.No.name());
                    break;
                case Used:
                    condition.put("is_use", YesOrNo.Yes.name());
                    break;
                case Expired:
                    condition.put("is_use", YesOrNo.No.name());
                    condition.put("is_overdue", YesOrNo.Yes.name());
                    break;
            }
        }
        String pageSql = condition.getFullSql("get_user_coupons_page");
        String countSql = condition.getFullSql("get_user_coupons_count");
        Page<CouponsUserItemVO> page = couponsService.getViewModelPageBySql(new Page(pageNo, pageSize),
                pageSql, countSql, CouponsUserItemVO.class, condition.getParams().toArray());
        return page;
    }



    /**
     *
     * @param dto
     * @return
     */
    public Page<MyCouponsVO> geGoodsPage(MyCouponsPageDTO dto){
        Page<MyCouponsVO> page = new Page(dto);

        String condition = "";
        if(null != dto.getStatus()){
            condition = dto.getStatus().getSql();
        }
        String type = "%";
        if(null != dto.getType()){
            type = dto.getType().name();
        }

        String pageSql = SystemSQL.getInstance().getSystemSQL("get_my_coupons_page").replace("[condition]", condition);
        String countSql = SystemSQL.getInstance().getSystemSQL("get_my_coupons_count").replace("[condition]", condition);

        page = couponsService.getViewModelPageBySql(page, pageSql, countSql, MyCouponsVO.class,
                new String[]{userService.get().getMemberUserId(), userService.getComId(),type});
        return page;
    }

}
